package com.example.studykotlin.kotlin_actual_combat.Advanced.view;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class CustomView extends View {
    private int lastX = 0;
    private int lastY = 0;
    private Context mContext;
    private Scroller mScroll;

    @RequiresApi(api = Build.VERSION_CODES.M)
    public CustomView(Context context) {
        super(context);
        mContext = context;
        mScroll = new Scroller(context);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mScroll = new Scroller(context);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mScroll = new Scroller(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int offsetX =  x - lastX;
                int offsetY =  y - lastY;
//                useLayout(offsetX, offsetY);
//                useOffset(offsetX,offsetY);
//                getMyParent().scrollBy(-offsetX,-offsetY);

                break;
            default:
                break;
        }
        return true;
    }

    private void useLayout(int offsetX, int offsetY) {
        layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);
    }

    private void useOffset(int offsetX, int offsetY) {
        offsetLeftAndRight(offsetX);
        offsetTopAndBottom(offsetY);
    }

    private void useLayoutParams(int offsetX, int offsetY, boolean prent) {
        if (prent) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
            params.leftMargin = getLeft() + offsetX;
            params.topMargin = getRight() + offsetY;
            setLayoutParams(params);
        } else {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) getLayoutParams();
            params.leftMargin = getLeft() + offsetX;
            params.topMargin = getTop() + offsetY;
            setLayoutParams(params);
        }
    }

    public void useScroll(int destX,int destY) {
        int scrollX = getScrollX();
        int delta = destX - scrollX;
        try {
            mScroll.startScroll(scrollX,0,delta,0,2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        invalidate();
    }

    private View getMyParent() {
        return ((View)getParent());
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroll.computeScrollOffset()) {
            getMyParent().scrollTo(mScroll.getCurrX(),mScroll.getCurrY());
            postInvalidate();
        }
    }


}
